// friend_database.cpp
#include <QSqlError>
#include <QDebug>
#include"friend_database.h"
#include"QJsonObject"
FriendDatabase::FriendDatabase(DatabaseConnection *dbConnection, QObject *parent)
    : QObject(parent), dbConnection(dbConnection) {}

bool FriendDatabase::addFriend(int userId1, int userId2) {
    QSqlDatabase db = dbConnection->database();
    QSqlQuery query(db);

    // 确保 user_id1 < user_id2 以避免重复记录
    if (userId1 > userId2) {
        std::swap(userId1, userId2);
    }
    qDebug() << "【好友数据库】添加好友";
    QString sql = "INSERT INTO friendships (user_id1, user_id2) VALUES (:user_id1, :user_id2)";
    query.prepare(sql);
    query.bindValue(":user_id1", userId1);
    query.bindValue(":user_id2", userId2);

    if (!query.exec()) {
        qDebug() << "【好友数据库】添加好友失败：" << query.lastError().text();
        return false;
    }

    return true;
}

bool FriendDatabase::removeFriend(int userId1, int userId2) {
    QSqlDatabase db = dbConnection->database();
    QSqlQuery query(db);

    if (userId1 > userId2) {
        std::swap(userId1, userId2);
    }

    QString sql = "DELETE FROM friendships WHERE user_id1 = :user_id1 AND user_id2 = :user_id2";
    query.prepare(sql);
    query.bindValue(":user_id1", userId1);
    query.bindValue(":user_id2", userId2);

    if (!query.exec()) {
        qDebug() << "【好友数据库】删除好友失败：" << query.lastError().text();
        return false;
    }

    return true;
}

QList<int> FriendDatabase::getFriendList(int userId) {
    QList<int> friendList;
    QSqlDatabase db = dbConnection->database();
    QSqlQuery query(db);

    QString sql = "SELECT user_id1, user_id2 FROM friendships WHERE user_id1 = :user_id OR user_id2 = :user_id";
    query.prepare(sql);
    query.bindValue(":user_id", userId);

    if (!query.exec()) {
        qDebug() << "【好友数据库】获取好友列表失败：" << query.lastError().text();
        return friendList;
    }

    while (query.next()) {
        int friendId1 = query.value(0).toInt();
        int friendId2 = query.value(1).toInt();
        if (friendId1 != userId) {
            friendList.append(friendId1);
        } else {
            friendList.append(friendId2);
        }
    }

    return friendList;
}

QList<QJsonObject> FriendDatabase::getFriendsList(int userId) {
    QList<QJsonObject> friendList;
    QSqlDatabase db = dbConnection->database();
    QSqlQuery query(db);

    // 查询包含该用户的所有好友关系记录，并关联 users 表获取好友的用户名
    QString sql = "SELECT u.id, u.username "
                  "FROM friendships f "
                  "JOIN users u ON (f.user_id1 = u.id AND f.user_id2 = :user_id) "
                  "OR (f.user_id2 = u.id AND f.user_id1 = :user_id)";
    query.prepare(sql);
    query.bindValue(":user_id", userId);

    if (!query.exec()) {
        qDebug() << "【好友数据库】获取好友列表失败：" << query.lastError().text();
        return friendList;
    }

    // 遍历查询结果，构建 QJsonObject
    while (query.next()) {
        int friendId = query.value(0).toInt();
        QString friendUsername = query.value(1).toString();

        QJsonObject friendObj;
        friendObj["id"] = friendId;
        friendObj["username"] = friendUsername;

        friendList.append(friendObj);
    }

    return friendList;
}
